#include "responsivefactory.h"
#include "customfilelistwidget.h"
#include <QDebug>

ResponsiveFactory& ResponsiveFactory::instance()
{
    static ResponsiveFactory instance;
    return instance;
}

ResponsiveFileListEngine* ResponsiveFactory::createResponsiveEngine(QObject* parent)
{

    auto* engine = new ResponsiveFileListEngine(parent);

    // 应用Sony设计默认配置
    applySonyDesignDefaults(engine);

    // 应用性能优化
    if (m_performanceOptimization) {
        applyPerformanceOptimizations(engine);
    }

    return engine;
}

AdaptiveFileLayout* ResponsiveFactory::createAdaptiveLayout(QObject* parent)
{

    auto* layout = new AdaptiveFileLayout(parent);

    // 配置自适应布局参数
    layout->setAnimationEnabled(m_animationEnabled);
    layout->setPerformanceMode(m_performanceOptimization);

    // 设置Sony风格的间距和对齐
    layout->setItemSpacing(ResponsiveConfig::Performance::ENABLE_LAYOUT_CACHING ? 2 : 3);
    layout->setMargin(8);

    return layout;
}

ResponsiveFileItem* ResponsiveFactory::createResponsiveFileItem(const UnifiedFileInfo& fileInfo,
                                                                 ResponsiveFileListEngine* engine,
                                                                 QWidget* parent)
{

    auto* item = new ResponsiveFileItem(fileInfo, engine, parent);

    // 应用Sony设计风格
    item->setHoverEnabled(true);
    item->setAnimationEnabled(m_animationEnabled);

    return item;
}

void ResponsiveFactory::applySonyDesignDefaults(ResponsiveFileListEngine* engine)
{
    if (!engine) {
        return;
    }


    // 设置显示模式阈值
    engine->setModeThreshold(SonyIveDesign::DisplayMode::Micro,
                            ResponsiveConfig::SonyDesign::MICRO_MODE_THRESHOLD);
    engine->setModeThreshold(SonyIveDesign::DisplayMode::Compact,
                            ResponsiveConfig::SonyDesign::COMPACT_MODE_THRESHOLD);
    engine->setModeThreshold(SonyIveDesign::DisplayMode::Standard,
                            ResponsiveConfig::SonyDesign::STANDARD_MODE_THRESHOLD);
    engine->setModeThreshold(SonyIveDesign::DisplayMode::Expanded,
                            ResponsiveConfig::SonyDesign::EXPANDED_MODE_THRESHOLD);

    // 配置动画参数
    engine->setAnimationDuration(ResponsiveConfig::SonyDesign::ANIMATION_DURATION);
    engine->setHoverAnimationDuration(ResponsiveConfig::SonyDesign::HOVER_ANIMATION_DURATION);
    engine->setSelectionAnimationDuration(ResponsiveConfig::SonyDesign::SELECTION_ANIMATION_DURATION);

    // 配置缩放因子
    engine->setHoverScaleFactor(ResponsiveConfig::SonyDesign::HOVER_SCALE_FACTOR);
    engine->setSelectionScaleFactor(ResponsiveConfig::SonyDesign::SELECTION_SCALE_FACTOR);

    // 启用Sony风格特性
    engine->setSmoothTransitions(true);
    engine->setProgressiveInformationDisclosure(true);
}

void ResponsiveFactory::applyPerformanceOptimizations(ResponsiveFileListEngine* engine)
{
    if (!engine) {
        return;
    }


    // 设置缓存和节流参数
    engine->setCacheUpdateDelay(ResponsiveConfig::Performance::CACHE_UPDATE_DELAY);
    engine->setLayoutUpdateDelay(ResponsiveConfig::Performance::LAYOUT_UPDATE_DELAY);
    engine->setAnimationThrottleDelay(ResponsiveConfig::Performance::ANIMATION_THROTTLE_DELAY);
    engine->setMaxCachedItems(ResponsiveConfig::Performance::MAX_CACHED_ITEMS);

    // 启用各种缓存
    engine->setBackgroundCachingEnabled(ResponsiveConfig::Performance::ENABLE_BACKGROUND_CACHING);
    engine->setLayoutCachingEnabled(ResponsiveConfig::Performance::ENABLE_LAYOUT_CACHING);
    engine->setTextMetricsCachingEnabled(ResponsiveConfig::Performance::ENABLE_TEXT_METRICS_CACHING);
}

void ResponsiveFactory::enableResponsiveMode(CustomFileListWidget* widget, bool enabled)
{
    if (!widget) {
        return;
    }


    // 如果需要，创建响应式引擎
    if (enabled && !widget->getResponsiveEngine()) {
        auto* engine = createResponsiveEngine(widget);
        widget->setResponsiveEngine(engine);
    }

    // 启用/禁用响应式模式
    widget->enableResponsiveMode(enabled);
}

void ResponsiveFactory::setGlobalResponsiveMode(bool enabled)
{
    if (m_globalResponsiveMode != enabled) {
        m_globalResponsiveMode = enabled;
    }
}

bool ResponsiveFactory::isGlobalResponsiveModeEnabled() const
{
    return m_globalResponsiveMode;
}

void ResponsiveFactory::setAnimationEnabled(bool enabled)
{
    if (m_animationEnabled != enabled) {
        m_animationEnabled = enabled;
    }
}

bool ResponsiveFactory::isAnimationEnabled() const
{
    return m_animationEnabled;
}